<layout name="layout"/>
<section class="content-header">
    <h1>
        菜单权限
    </h1>
</section>

<div class="content">
    <div class="box box-info">
        <form id="form" action="" class="form-horizontal" method="post">
            <input style="display: none" value="{$id}" name="id" id="id">
            <input style="display: none"
                   id="menuids" name="menuids" value="{$menuids}">

            <ul id="tree" class="ztree" style="width:560px; overflow:auto;"></ul>
            <script>
                var setting = {
                    check: {
                        enable: true
                    },
                    view: {
                        dblClickExpand: false,
                        showLine: true,
                        selectedMulti: false
                    },
                    data: {
                        simpleData: {
                            enable: true,
                            idKey: "id",
                            pIdKey: "pId",
                            rootPId: ""
                        }
                    },
                    callback: {
                        onCheck: function (event, treeId, treeNode) {
                            var treeObj = $.fn.zTree.getZTreeObj("tree");
                            debugger;
                            var nodes = treeObj.getCheckedNodes(true);
                            var ids = "";
                            $(nodes).each(function () {

                                if(this.pId!=''){
                                    ids += this.id + ",";
                                }

                            });
                            $('#menuids').val(ids);
                        }
                    }
                };

                var zNodes = [
                <volist name="menulist" id="vo">
                    {id: "{$vo.id}", pId: "{$vo.parentid}", name: "{$vo.name}", open: true},
                </volist>
                ];

                $(document).ready(function () {

                    var t = $("#tree");
                    t = $.fn.zTree.init(t, setting, zNodes);
                    var zTree = $.fn.zTree.getZTreeObj("tree");
                    var cueentMenuids = '{$menuids}';
                    if (cueentMenuids != '') {
                        var nodeids = cueentMenuids.split(",");
                        debugger;
                        $(nodeids).each(function (index, entity) {
                            var node = zTree.getNodeByParam('id',entity);
                            zTree.checkNode(node, true, true);
                        });
                    }
                });
            </script>
            <div class="box-footer">
                <a href="javascript:void(0)" onclick="commonSubmit('form','__APP__/home/role/configMenuEntity','')"
                   class="btn btn-primary pull-right" style="margin-left: 5px">提交</a>
                <a href="__APP__/home/role" class="btn btn-default  pull-right">返回</a>
            </div>
            <!-- /.box-footer -->
        </form>
    </div>
</div>
<script>
    $(document).ready(function () {
        var userids = '{$userids}';
        var ids = userids.split(',');
        $(ids).each(function () {
            $('#' + this).prop('checked', true);
        })
    });
</script>
